<?php
namespace app\index\controller;

use app\index\model\Admin;
use think\Db;
use think\facade\Request;

class Index
{
    public function index()
    {
        $id = Request::get('id');
        if (empty($id)){
            return 'not request params';
        }
        return Db::connect([
            'type'            => 'mysql',
            'hostname'        => '192.168.1.174',
            'database'        => 'zk_viptjw_2',
            'username'        => 'zk_tjw',
            'password'        => '111111'
        ])->table('tj_admin')
            ->where('id', $id)
            ->value('username');
    }
    # dns方式连接
    public function index2(){
        $id = Request::get('id');
        if (empty($id)){
            return 'not request params';
        }
        $dns = 'mysql://zk_tjw:111111@192.168.1.174:3306/zk_viptjw_2#utf8';
        return Db::connect($dns)
            ->table('tj_admin')
            ->where('id', $id)
            ->value('username');
    }

    # 使用db类查询
    public function index3(){
        $dns = 'mysql://zk_tjw:111111@192.168.1.174:3306/zk_viptjw_2#utf8';
        # 查询多个
//        $res = Db::connect($dns)->table('tj_admin')->field(['id','username'=>'管理员','mob'=>'手机'])->where('id','>','40')->select();
        # 查询单个
//        $res = Db::connect($dns)->table('tj_admin')->field(['id','username'=>'管理员','mob'=>'手机'])->find(46);
        # 多个查询条件
        $res = Db::connect($dns)->table('tj_admin')->field(['id','username'=>'管理员','mob'=>'手机'])
            ->where([
                ['role_id','=',30],
                ['logincount','>',60]
            ])->select();
        dump($res);
    }
    # 插入单条
    public function insert(){
        $data = [
            'msg'=>'事实上222'.date('H:i:s')
        ];
        $dns = 'mysql://zk_tjw:111111@192.168.1.174:3306/zk_viptjw_2#utf8';
//        return Db::name('error_log')->insert($data,true);
//        return Db::name('error_log')->data($data)->insert();
        # 返回新增的id
        return Db::name('error_log')->insertGetId($data);
    }
    # 插入多条
    public function insertall(){
        $data = [
            ['msg'=>'随机数='.rand(100,999)],
            ['msg'=>'随机数='.rand(100,999)],
            ['msg'=>'随机数='.rand(100,999)],
            ['msg'=>'随机数='.rand(100,999)]
        ];
        return Db::name('error_log')->data($data)->insertAll();
    }
    # 更新操作
    public function update(){
//        return Db::name('error_log')
//            ->where('id', 90179)
//            ->update(['msg'=>rand(3,9)]);
        # 如果是主键的可以这样写
        return Db::name('error_log')
            ->update([
                'id'=>90180,
                'msg'=>rand(5000,6000)
            ]);
    }
    # 删除操作
    public function delete(){
        return Db::name('error_log')
            ->delete(90174);
    }
    # 原生查询
    public function query(){
        $sql = 'select * from tj_error_log';
        $res = Db::query($sql);
        dump($res);
    }
    # 原生写操作
    public function execute(){
        $sql = "insert  tj_error_log set msg='aadd'";
        $sql = "update tj_error_log set msg='aabbdd' where id=90182";
        return Db::execute($sql);
    }


    # 模型操作
    public function index11(){
        $res = Admin::get(1);
        dump($res instanceof Admin);die;
//        $res = Admin::all([1,46,47]);
        # 复杂查询
//        $res = Admin::field('id,username')->select();
//        $res = Admin::field('id,username,mob')->where('id', 'in', [1,46,48])->select();
        dump($res);
    }
    public function hello($name = 'ThinkPHP5')
    {
        return 'hello,' . $name;
    }
}
